using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using MinimalistFoodStreet.BackEnd.Infrastructure.Attributes;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Request.Tables;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Response;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Response.Tables;
using MinimalistFoodStreet.BackEnd.Infrastructure.EFCore;
using MinimalistFoodStreet.BackEnd.Server.Server.RequestExtend.Tables;
using MinimalistFoodStreet.BackEnd.Server.Server.ResponseExtend.Tables;
using MinimalistFoodStreet.BackEnd.Server.IServer.Tables;

namespace MinimalistFoodStreet.BackEnd.Server.Server.Tables
{
    [InjectClass]
    [CheckLogin(new CheckLoginRoles[] { CheckLoginRoles.administrators })]
    public class DictionaryTypesServer : IDictionaryTypesServer
    {
        private readonly ServerRepository _serverRepository;
        public DictionaryTypesServer(DbContext dbContext)
        {
            _serverRepository = new ServerRepository(dbContext);
        }

        public async Task<ResponseApiDto> AddAsync(RequestCreateDictionaryTypesDto dto)
        {
            if (await _serverRepository.dictionaryTypesEFCore.Query(d => d.DtKey == dto.key).AnyAsync())
            {
                return ResponseApiDto.Fail($"键值\"{dto.key}\"已存在");
            }
            int affectedRows = await _serverRepository.dictionaryTypesEFCore.AddAsync(dto.ToCreateEntity());
            return ResponseApiDto.Create(affectedRows);
        }

        public async Task<ResponseApiDto> UpdateAsync(RequestUpdateDictionaryTypesDto dto)
        {
            if (await _serverRepository.dictionaryTypesEFCore.Query(d => d.DtKey == dto.key && d.DtId != dto.id).AnyAsync())
            {
                return ResponseApiDto.Fail($"键值\"{dto.key}\"已存在");
            }
            DictionaryType dictionaryType = await _serverRepository.dictionaryTypesEFCore.Query(d => d.DtId == dto.id).SingleOrDefaultAsync();
            if (dictionaryType == null)
            {
                return ResponseApiDto.Fail("该实例不存在");
            }
            dictionaryType.DtName = dto.name;
            dictionaryType.DtKey = dto.key;
            dictionaryType.DtDescription = dto.description;
            int affectedRows = await _serverRepository.dictionaryTypesEFCore.UpdateAsync(dictionaryType);
            return ResponseApiDto.Create(affectedRows);
        }

        public async Task<ResponseApiDto> DeleteAsync(RequestDeleteDictionaryTypesDto dto)
        {
            int childrenCount = await _serverRepository.dictionarysEFCore.Query(d => d.DDictionaryTypeId == dto.id).CountAsync();
            if (childrenCount > 0)
            {
                return ResponseApiDto.Fail("不能删除有子级的字典类别，子级数：" + childrenCount);
            }
            int affectedRows = await _serverRepository.dictionaryTypesEFCore.Query(d => d.DtId == dto.id).ExecuteDeleteAsync();
            if (affectedRows == 0)
            {
                return ResponseApiDto.Fail("该实例不存在");
            }
            return ResponseApiDto.Create(affectedRows);
        }

        public async Task<ResponseApiDto> DetailAsync(int id)
        {
            DictionaryType dictionaryType = await _serverRepository.dictionaryTypesEFCore.Query(d => d.DtId == id).SingleOrDefaultAsync();
            if (dictionaryType == null)
            {
                return ResponseApiDto.Fail("该实例不存在");
            }
            return ResponseApiDto.Ok("数据已获取", dictionaryType.ToResponseDto());
        }

        public async Task<ResponseApiDto> PaginationAsync(int page, int limit, string? key)
        {
            int total = 0;
            List<DictionaryType> dictionaryTypes = await _serverRepository.dictionaryTypesEFCore.Pagination(d => key == null || d.DtName.Contains(key) || d.DtKey.Contains(key), false, d => d.DtId, out total, page, limit).ToListAsync();
            List<ResponseDictionaryTypesDto> responseDictionaryTypesDtos = dictionaryTypes.Select(d => d.ToResponseDto()).ToList();
            return ResponseApiDto.Ok("数据已获取", new { data = responseDictionaryTypesDtos, total = total });
        }
    }
}